package xeroxware.framework.xplatform;

import java.util.Map;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.web.servlet.view.AbstractView;

import com.tobesoft.xplatform.data.PlatformData;
import com.tobesoft.xplatform.tx.HttpPlatformResponse;

import xeroxware.framework.util.CommonUtil;

/**
 * <pre>
 * XplatformView
 * - 처리결과를 PlatformResponse 객체를 이용하여 전송한다.
 * </pre>
 * 
 * @since 2013. 1. 9.
 * @author XEROXER
 */
public class XplatformView extends AbstractView {

	@Value("#{contextProperties['xplatform.content.type']}")
	private String xpContentType;

	@Value("#{contextProperties['xplatform.protocol.type']}")
	private String xpProtocolType;

	@Value("#{contextProperties['xplatform.charset']}")
	private String xpCharset;

	@Value("#{contextProperties['xplatform.dto.id']}")
	private String xpDtoId;

	private static final Logger logger = LoggerFactory.getLogger(XplatformView.class);

	@Override
	protected void renderMergedOutputModel(Map<String, Object> model, HttpServletRequest request,
			HttpServletResponse response) throws Exception {

		XplatformDTO xplatformDTO = null;
		PlatformData platformData = null;
		HttpPlatformResponse platformResponse = null;

		try {
			xplatformDTO = (XplatformDTO) model.get(xpDtoId);
			xplatformDTO.addVariable("gv_TransDt", CommonUtil.getDateTime());
			platformData = xplatformDTO.getPlatformData();

			logger.debug("========== Response PlatformData ==========\n{}", platformData.saveXml());

			platformResponse = new HttpPlatformResponse(response, xpContentType, xpCharset);
			platformResponse.addProtocolType(xpProtocolType);
			platformResponse.setData(platformData);
			platformResponse.sendData();
		} catch (Exception e) {
			logger.error("[{}] {}", e.getClass().getSimpleName(), e.getMessage(), e);
		}
	}
}
